In the addon of the session, we calculated the distances to the closest hospitals located within North-Rhine Westphalia (NRW) and Baden Wurttemberg (BW). Still, we did not show how to subset the original file, which contains all hospitals in Germany.
Subset the data file yourself by relying on the spatial information of the filehospital_points.csv and a polygon of NRW only. How many hospitals are located within the borders of NRW?
hospital_points.csv in the ./data folder and polygons of NRW. For the latter, you can again use the osmdata syntax.
The default of sf::st_join() will leave you with a ‘left-join’ and returns a data object with all hospitals and matching district information for those which are located within NRW. You can reset the option to perform an ‘inner-join’ and keep only the observation which lay within the predefined area (sf::st_join(x , y, join = "", left = FALSE)).
# load hospitals
hospitals <-
read.csv(
"./data/hospital_points.csv",
header = TRUE,
fill = TRUE,
sep = ","
) %>%
sf::st_as_sf(coords = c("X", "Y"), crs = 3035)
# use the OSM function
nrw <-
osmdata::getbb(
"Nordrhein-Westfalen",
format_out = "sf_polygon"
) %>%
.$multipolygon %>%
sf::st_transform(3035)
# spatial join
nrw_hospitals <-
hospitals %>%
sf::st_join(
# point layer nrw
nrw,
# chose intersect or within
join = sf::st_intersects,
# option FALSE will
# keep only the hospital
# which could be joined
left = FALSE
)
nrw_hospitals
## Simple feature collection with 344 features and 4 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 4039950 ymin: 3058862 xmax: 4277224 ymax: 3246338
## Projected CRS: ETRS89-extended / LAEA Europe
## First 10 features:
## name year beds
## 319 Ev. Krankenhaus 2017 568
## 320 Fliedner Klinik Ambulanz und Tagesklinik f\xfcr Psychiatrie, Psychotherapie und Psychosomatik 2017 .
## 321 Johanniter-Tagesklinik Klinik f\xfcr Psychiatrie und Psychotherapie 2017 .
## 322 Krankenanstalten Florence Nightingale 2017 568
## 323 Krankenhaus M\xf6rsenbroich-Rath 2017 718
## 324 LVR-Klinikum D\xfcsseldorf Klinikum der Heinrich-Heine- Universit\xe4t D\xfcsseldorf 2017 495
## 325 Marien-Hospital 2017 461
## 326 Medical Center D\xfcsseldorf -Luisenkrankenhaus- 2017 42
## 327 Paracelsus Klinik Golzheim Fachklinik f\xfcr Urologie und Kinderurologie 2017 81
## 328 Sana Kliniken D\xfcsseldorf 2017 490
## ags geometry
## 319 5111000 POINT (4095599 3127379)
## 320 5111000 POINT (4096285 3128719)
## 321 5111000 POINT (4101713 3121737)
## 322 5111000 POINT (4094348 3137304)
## 323 5111000 POINT (4099466 3133197)
## 324 5111000 POINT (4100738 3130402)
## 325 5111000 POINT (4096526 3129923)
## 326 5111000 POINT (4099035 3129691)
## 327 5111000 POINT (4094841 3132032)
## 328 5111000 POINT (4101452 3130494)
# 344 hospitals in NRW
Did the operationalization of health care provision convince you? Don’t you think it might be more important how many hospitals are close to survey respondents? To test this, we want to calculate the number of hospitals per district in North-Rhine Westphalia. Use the syntax below to prep the hospital data.
Earn extra points by counting not only the number of hospitals but also the sum of hospital beds within a district.dplyr::as_tibble() data frame to use the functions dplyr::group_by() and dplyr::summarise().
dplyr::n() allows summarizing the total count of hospitals. sum(beds) for summarizing the bed total per district.
nrw_districts <-
sf::read_sf("./data/VG250_KRS.shp") %>%
sf::st_transform(3035) %>%
sf::st_join(nrw, join = sf::st_intersects, left = FALSE)
nrw_hospitals <-
nrw_hospitals %>%
# beds were character, now numeric
dplyr::mutate(beds = as.numeric(beds)) %>%
# replace NAs as zeros for simplification
replace(is.na(.), 0)
## Warning in mask$eval_all_mutate(quo): NAs introduced by coercion
district_hospital_join <-
nrw_hospitals %>%
# join the hospitals
# within districts
sf::st_join(nrw_districts, join = sf::st_within) %>%
# use as tibble to perform
# group by & summarise
dplyr::as_tibble() %>%
dplyr::group_by(AGS) %>%
dplyr::summarise(
hospital_count = dplyr::n(),
hospital_bed_count = sum(as.numeric(beds))
) %>%
# left join the new information
# to the original data frame
dplyr::left_join(nrw_districts, .)
## Joining, by = "AGS"
district_hospital_join
## Simple feature collection with 73 features and 25 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 4031313 ymin: 2972671 xmax: 4332216 ymax: 3337853
## Projected CRS: ETRS89-extended / LAEA Europe
## # A tibble: 73 × 26
## ADE GF BSG ARS AGS SDV_ARS GEN BEZ IBZ BEM NBD SN_L SN_R SN_K SN_V1 SN_V2 SN_G FK_S3
## <int> <int> <int> <chr> <chr> <chr> <chr> <chr> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 4 4 1 03155 03155 03155001… Nort… Land… 43 -- ja 03 1 55 00 00 000 K
## 2 4 4 1 03251 03251 03251001… Diep… Land… 43 -- ja 03 2 51 00 00 000 K
## 3 4 4 1 03252 03252 03252000… Hame… Land… 43 -- ja 03 2 52 00 00 000 K
## 4 4 4 1 03255 03255 03255002… Holz… Land… 43 -- ja 03 2 55 00 00 000 K
## 5 4 4 1 03256 03256 03256002… Nien… Land… 43 -- ja 03 2 56 00 00 000 K
## 6 4 4 1 03257 03257 03257003… Scha… Land… 43 -- ja 03 2 57 00 00 000 K
## 7 4 4 1 03404 03404 03404000… Osna… Krei… 40 -- ja 03 4 04 00 00 000 K
## 8 4 4 1 03454 03454 03454003… Emsl… Land… 43 -- ja 03 4 54 00 00 000 K
## 9 4 4 1 03456 03456 03456001… Graf… Land… 43 -- ja 03 4 56 00 00 000 K
## 10 4 4 1 03459 03459 03404000… Osna… Land… 43 -- ja 03 4 59 00 00 000 K
## # … with 63 more rows, and 8 more variables: NUTS <chr>, ARS_0 <chr>, AGS_0 <chr>, WSK <date>, DEBKG_ID <chr>,
## # geometry <MULTIPOLYGON [m]>, hospital_count <int>, hospital_bed_count <dbl>